package com.itheima.controller.backend.order;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Api(tags = "检查组管理")
@Slf4j
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /*
    * 新增检查组
    * */
    @PostMapping("/add")
    @ApiOperation(value = "新增检查组")
    public Result add(@RequestBody CheckGroup checkGroup,@RequestParam List<Integer> checkitemIds) {
        log.info("新增检查组:{},{}", checkGroup, checkitemIds);
        checkGroupService.add(checkGroup, checkitemIds);
        return new Result(true, "新增检查组成功", null);
    }

    /*
    * 根据id删除检查组
    * */
    @GetMapping("/deleteById")
    @ApiOperation(value = "根据id删除检查组")
    public Result deleteById(Integer id) {
        log.info("根据id删除检查组:{}", id);
        checkGroupService.deleteById(id);
        return new Result(true, "删除检查组成功", null);
    }

    /*
    * 根据id修改检查组
    * */
    @PostMapping("/edit")
    @ApiOperation(value = "根据id修改检查组")
    public Result edit(@RequestBody CheckGroup checkGroup,@RequestParam List<Integer> checkitemIds) {
        log.info("根据id修改检查组:{},{}", checkGroup, checkitemIds);
        checkGroupService.update(checkGroup, checkitemIds);
        return new Result(true, "修改检查组成功", null);
    }

    /*
    * 查询所有检查组
    * */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有检查组")
    public Result findAll(){
        log.info("查询所有检查组");
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return new Result(true, "查询检查组成功", checkGroups);
    }

    /*
    * 根据id查询检查组
    * */
    @GetMapping("/findById")
    @ApiOperation(value = "根据id查询检查组")
    public Result findById(Integer id){
        log.info("根据id查询检查组:{}",id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true, "查询成功" ,checkGroup);
    }

    /*
    * 分页查询检查组
    * */
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询检查组")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查组:{}",queryPageBean);
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }

    /*
    * 根据套餐id查询关联的检查项id
    * */
    @GetMapping("//findCheckGroupIdsBySetmealId")
    @ApiOperation(value = "根据套餐id查询关联的检查项id")
    public Result findCheckGroupIdsBySetmealId(Integer setmealId){
        log.info("根据套餐id查询关联的检查项id:{}",setmealId);
        List<Integer> checkGroupIds = checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(true, "查询成功", checkGroupIds);
    }
}
